<template>
  <div>
    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级类别" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="options"
                :normalizer="normalizer"
                placeholder="选择上级类别"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类别编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入类别编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入类别名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="order">
              <el-input-number
                v-model="form.order"
                controls-position="right"
                :min="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="() => (this.open = false)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, pageList as option, add, edit } from "@/api/prod/purchase/asset-type";
import { handleOptions } from "@/utils/base/options";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AddAssetType",
  dicts: ["sys_normal_disable"],
  components: { Treeselect },
  data() {
    return {
      // 表单类型
      type: null,
      // 部门树选项
      deptOptions: [],
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        id: null,
        code: null,
        name: null,
        order: 0,
        parentId: null,
        remark: null,
        status: null,
      },
      // 表单校验
      rules: {},
      // 选项
      options: [],
    };
  },

  created() {
    // 资产类别
    option({}).then((response) => {
      this.options = handleOptions(response.data);
    });
  },

  computed: {
    // 弹出层标题
    title() {
      if (this.type !== null) return this.type + "-固定资产类别";
      return "";
    },
  },

  methods: {
    // 表单重置
    reset() {
      this.type = null;
      this.form = {
        id: null,
        code: null,
        name: null,
        order: 0,
        parentId: null,
        remark: null,
        status: null,
      };
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },

    /** 弹出框打开 */
    show(type, id, parentId) {
      this.reset();
      this.open = true;
      this.type = type;
      this.form.parentId = parentId;
      if (id != null && id != undefined) {
        get(id).then((response) => {
          this.form = response.data;
        });
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.type == "修改") {
            edit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("refresh");
            });
          } else {
            add(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("refresh");
            });
          }
        }
      });
    },
  },
};
</script>

<style>
</style>